//
//  ActViewController.m
//  MyProject
//
//  Created by dllo on 15/11/6.
//  Copyright (c) 2015年 蓝鸥0825. All rights reserved.
//


//      最 in 活动里的二级页面
#import "ActViewController.h"
#import "ActView.h"
#import "Cache.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface ActViewController ()

@end

@implementation ActViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    ActView *view = [[ActView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:view];
    view.modelAct = self.modelAct;
    NSLog(@"%@", self.modelAct);
    [[Cache shareDataBaseHandle] openDB];
    [[Cache shareDataBaseHandle] createTable];
    [[Cache shareDataBaseHandle] isHaveInTable:self.modelAct];
    
    //      用一个枚举类型的变量接收结果, 变量去比较
    SelectMovie result = [[Cache shareDataBaseHandle] isHaveInTable:self.modelAct];
    
    
    if (result == InTable) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"已收藏" style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
    } else if (result == NotInTable){
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
    } else {
        NSLog(@"错误");
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



- (void)click:(UIBarButtonItem *)button{
    
    if ([button.title isEqualToString:@"收藏"]) {
        [[Cache shareDataBaseHandle] saveMovieInDB:self.modelAct];
        button.title = @"已收藏";
    
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示信息" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        [alert addAction:cancel];
        [self presentViewController:alert animated:YES completion:^{
        }];


    } else {
    
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示信息" message:@"是否取消收藏" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        [alert addAction:cancel];
        [self presentViewController:alert animated:YES completion:^{
        }];
        
        
        [[Cache shareDataBaseHandle] deleteMovieInDB:self.modelAct];
        button.title = @"收藏";
    }
        [[Cache shareDataBaseHandle] selectAllModel];
    
}





@end
